package com.leather.express.controller;

import com.leather.common.entity.User;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.express.feignclient.UserClient;
import com.leather.express.service.ExpressSevice;
import com.leather.express.vo.ExpressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExpressController {


    @Autowired
    private UserClient userClient;

    @Autowired
    private ExpressSevice expressSevice;


    @GetMapping("/index")
    public Result index(int pageId) {
        return expressSevice.expressIndex(pageId * 20);
    }

    @PostMapping("/add")
    public Result add(ExpressVo expressVo) {
        return expressSevice.addExpress(expressVo);
    }

    @PostMapping("/getexpress")
    public Result getExpress(int expressId, int userId) {
        return expressSevice.getExpress(expressId, userId);
    }

    @GetMapping("/search")
    public Result search(String search) {
        return expressSevice.searchValue(search);
    }

    @GetMapping("/getuserorder")
    public Result getUserOrder(int pageId, int userId) {
        return expressSevice.queryUserGetExpress(pageId * 20, userId);
    }


    @PostMapping("/getresult")
    public Result getResult(int expressId) {
        return expressSevice.getSuccess(expressId);
    }

    @GetMapping("/getuserexpress")
    public Result getUserExpress(int pageId, int userId) {
        return expressSevice.queryUserExpress(pageId * 20, userId);
    }

    @PostMapping("getcontact")
    public Result contact(int userId) {
        User user = userClient.getUserById(userId);
        if (null == user) {
            return Result.error(CodeMsg.ERROR_CODE);
        }
        return Result.success(user.getPhone());
    }

    @PostMapping("/delete")
    public Result deleteUserExpress(int expressId, int userId) {
        return expressSevice.deleteUserExpress(expressId, userId);
    }

    @PostMapping("/geteps")
    public Result getExpressById(int expressId) {
        return expressSevice.getExpressById(expressId);
    }


}
